from generic.album import Album
from tidal.constants import IMG_URL
import tidal.track
import tidal.artist

class TidalAlbum(Album):
  def set_raw_data(self, raw_data):
    self.set_values(album_id = raw_data["id"],
                    title = raw_data["title"],
                    artists = list(map(
                      lambda a: tidal.artist.TidalArtist.grab_or_create(
                        a["id"], raw_data = a
                      ), raw_data["artists"]
                    )),
                    songs = list(map(
                      lambda s: tidal.track.TidalTrack.grab_or_create(
                        s["id"], raw_data = s
                      ), raw_data["songs"]["items"]
                    )),
                    is_full = True)

  @property
  def picture_url(self, width = 512, height = 512):
      return IMG_URL.format(width = width,
                            height = height,
                            id = self.album_id,
                            id_type = 'albumid')
  def set_values(self,
                 album_id = None,
                 title = None,
                 artists = None,
                 songs = None,
                 is_full = False, # if album is fully loaded
                 raw_data = None):
    if raw_data != None:
      self.set_raw_data(raw_data)
      return
    self.album_id = album_id
    self.title = title
    self.artists = artists
    self.songs = songs
    self.is_full = is_full
